package com.leixiaoqiao.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leixiaoqiao.mapper.BookMapper;
import com.leixiaoqiao.pojo.Book;
import com.leixiaoqiao.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description:
 * @company: 云桥之上
 * @author: 萌萌居家好男人
 * @version: 1.0
 * @create: 2021-11-21 20:19:06
 */
@Service//IOC的注解
@Transactional//事务管理
public class BookServiceImpl implements BookService {
    @Autowired
    private BookMapper bookMapper;


    @Override
    public List<Book> getBooks() {
        return bookMapper.queryAll();
    }

    @Override
    public PageInfo<Book> getPageBook(Integer pageNum, Integer pageSize) {
        //1.设置分页参数,底层会将设置的参数，通过拦截查询sql执行拼接limit
        PageHelper.startPage(pageNum,pageSize);
        //2.调用查询方法
        List<Book> bookList = bookMapper.queryAll();
        //3.组装返回pageInfo,注意构造函数
        return new PageInfo<>(bookList);
    }

    @Override
    public Book getBook(Integer id) {
        return bookMapper.selectOne(id);
    }

    @Override
    public Boolean addBook(Book book) {
        int rows = bookMapper.insert(book);
        if(rows > 0){
            return true;
        }
        return false;
    }

    @Override
    public Boolean updateBook(Book book){
        int rows = bookMapper.update(book);
        if(rows > 0){
            return true;
        }
        return false;
    }

    @Override
    public Boolean deleteBook(int id){
        int rows = bookMapper.delete(id);
        if(rows > 0){
            return true;
        }
        return false;
    }

    @Override
    public PageInfo<Book> page(Integer pageNum, Integer pageSize) {
        //1：设置分页参数
        PageHelper.startPage(pageNum,pageSize);//底层会将设置的参数，通过拦截查询sql执行拼接limit
        //2：调用查询方法（没有limit的查询sql）
        List<Book> bookList = bookMapper.queryAll();
        //3：组装返回PageInfo对象
        return new PageInfo<>(bookList);//注意构造参数
    }


}
